package com.mach.platform.api.controller;

import com.mach.platform.api.domain.Message;
import com.mach.platform.api.service.MessageServiceImpl;
import com.mach.platform.domain.PageVo;
import com.mach.platform.domain.Response;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 小程序消息页面接口
 */

@RequestMapping("/api/message")
@RestController
public class MessageCtrl {

    @Autowired
    private MessageServiceImpl messageService;

    /**
     * 获取分页的消息列表
     * @param request HttpServletRequest
     * @param hurdles 消息栏位：1.报警 2.运维 3.点检 4.账单 5.状态 6.公告
     * @param time 查询时间
     * @param pageable 分页参数
     * @return
     */
    @ApiOperation(value = "获取分页的消息列表")
    @PostMapping("/getMessageList")
    public Response getMessageList(HttpServletRequest request, int hurdles, String time, @PageableDefault(sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable) throws ParseException, UnsupportedEncodingException {
        String openId = request.getHeader("openId");
        PageVo pageVo = messageService.getMessageList(openId, null, hurdles, time, pageable);
        return Response.ok(pageVo) ;
    }


    /**
     * 获取消息详情
     * @param id 消息id
     * @return
     */
    @ApiOperation(value = "获取消息详情")
    @PostMapping("/messageInfo")
    public Response messageInfo(Long id) {
        if (id != null) {
            return Response.ok(messageService.info(id));
        }
        return Response.error("请求有误");
    }


    /**
     * 已读消息
     * @param request
     * @param id 消息id
     * @return
     */
    @ApiOperation(value = "已读消息")
    @PostMapping("/readMessage")
    public Response readMessage(HttpServletRequest request, Long id) {
        String openId = request.getHeader("openId");
        messageService.readMessage(openId, id);
        return Response.ok("消息已读");
    }


    /**
     * 删除消息
     * @param id 消息id
     * @return
     */
    @ApiOperation(value = "删除消息")
    @PostMapping("/deleteMessage")
    public Response deleteMessage(Long id) {
        int num = messageService.deleteMessage(id);
        if (num == 1) {
            return Response.ok("删除成功");
        } else {
            return Response.error("删除失败");
        }
    }


    /**
     * 设备分页消息列表
     * @param request
     * @param devId 设备id
     * @param hurdles 消息栏位
     * @param pageable 分页参数
     * @return
     */
    @ApiOperation(value = "设备消息列表")
    @PostMapping("/devMessageList")
    public Response devMessageList(HttpServletRequest request, Long devId, int hurdles, @PageableDefault(sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable) throws ParseException, UnsupportedEncodingException {
        String openId = request.getHeader("openId");
        PageVo pageVo = messageService.getMessageList(openId, devId, hurdles, null, pageable);
        return Response.ok(pageVo);
    }


    /**
     * 设备端发送文本消息
     * @param message 消息数据
     * @return
     */
    @ApiOperation(value = "设备端发送文本消息", produces = "application/json")
    @PostMapping("/sendMessage")
    public Response sendMessage(@RequestBody Message message) throws UnsupportedEncodingException {
        messageService.saveMessage(message);
        return Response.ok("发送消息成功");
    }


    /**
     * 设备端发送图片消息
     * @param request 请求信息
     * @param file 上传的图片
     * @return
     */
    @ApiOperation(value = "设备端发送图片消息")
    @PostMapping(value = "/sendImage")
    public Response sendImage(HttpServletRequest request, MultipartFile file){
        if (!messageService.sendImage(request, file)) {
            return Response.error("发送图片失败");
        }
        return Response.ok("发送图片成功");
    }


    /**
     * 获取当前用户可以@的团队成员
     * @param request
     * @return
     */
    @ApiOperation(value = "获取当前用户可以@的团队成员")
    @PostMapping("/getAllTeamMember")
    public Response getAllTeamMember(HttpServletRequest request) {
        String openId = request.getHeader("openId");
        List<Map> result = messageService.getAllTeamMember(openId);
        return Response.ok(result);
    }


}
